package tu.cs.cs386.controllers;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author S. Rongviriyapanish
 */
public class DBConnection {
    String url ;
    String user ;
    String password ;
    Connection dbConn ;
    
    public DBConnection(){
    
    }
    
    public DBConnection(String user, String pwd, String url){
    	this.user = user ;
    	this.password = pwd ;
    	this.url = url ;
    }

	public void openConnection() {
			try {
				Class.forName("com.mysql.jdbc.Driver");
				String fullURL =url+"&user="+user+"&password="+password;
				dbConn = DriverManager.getConnection(fullURL);	
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	}
    
    public void closeConnection(){
    	try {
			dbConn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public ResultSet executeStatement(String sqlCommand){
    	ResultSet results = null ;
    	try {
			Statement st = dbConn.createStatement();
			results = st.executeQuery(sqlCommand);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    	
        return results ;
    }
    
}